package common

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	commonDto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/common"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type SiteAnnouncementController struct {
	controller.BaseController
	siteAnnouncementService common.SiteAnnouncementServiceIface
}

func NewSiteAnnouncementController(
	siteAnnouncementService common.SiteAnnouncementServiceIface,
) *SiteAnnouncementController {
	return &SiteAnnouncementController{
		siteAnnouncementService: siteAnnouncementService,
	}
}

func (c *SiteAnnouncementController) RegisterRouter(g *gin.RouterGroup) {
	// 展示图形验证码
	g.GET("/site-announcement/list", c.list)
	// 发送验证码
	g.POST("/site-announcement/read", c.read)
}

func (c *SiteAnnouncementController) list(ctx *gin.Context) {
	request := commonDto.GetUserSiteAnnouncementListRequest{}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}
	list, err := c.siteAnnouncementService.GetUserSiteAnnouncementList(ctx, &request)
	if err != nil {
		stark.Logger.Info(ctx, "get-user-site-announcement-failed: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		stark.Logger.Errorf(ctx, "get-user-site-announcement-error:%+v", err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, list)
}

func (c *SiteAnnouncementController) read(ctx *gin.Context) {
	request := commonDto.SetSiteAnnouncementReadRequest{}
	if err := ctx.ShouldBindJSON(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}
	result, err := c.siteAnnouncementService.SetUserSiteAnnouncementRead(ctx, &request)
	if err != nil {
		stark.Logger.Info(ctx, "set-read-failed: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		stark.Logger.Errorf(ctx, "set-read-error:%+v", err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, result)
}
